package com.jwong.test;

import java.util.concurrent.*;

public class Demo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

       // final ScheduledFuture<?> scheduledFuture = executorService.scheduleAtFixedRate(new Task(), 3, 2, TimeUnit.SECONDS);
        final ScheduledFuture<Integer> schedule = executorService.schedule(new CallTask(executorService), 5, TimeUnit.SECONDS);

        System.out.println("===========getting value A");
        executorService.awaitTermination(1, TimeUnit.SECONDS);
        final Integer value = schedule.get();
        System.out.println("integerA = " + value);

        final ScheduledFuture<Integer> schedule1 = executorService.schedule(new CallTask(executorService), 5, TimeUnit.SECONDS);
        System.out.println("===========getting value B");
        final Integer valueB = schedule1.get();
        System.out.println("integerB = " + valueB);

    }

    private static class Task implements Runnable {

        @Override
        public void run() {
            System.out.println("RRRRRRRRRRRR");
        }
    }

    private static class CallTask implements Callable<Integer> {

        private  final ScheduledExecutorService executorService;

        int x = 0;

        private CallTask(ScheduledExecutorService executorService) {
            this.executorService = executorService;
        }

        @Override
        public Integer call() throws Exception {
            x++;
            return x;
        }
    }

}
